<?
$directory = str_replace('ajax','',dirname(__FILE__));
require_once($directory . "functions.php");
	if($_GET) {
		extract($_GET);
		$this_dir = root_dir . $top_dir;

		if($folder = opendir($this_dir)) {
			while(false !== ($file = readdir($folder))) {
				if($file != '.' && $file != '..') {
					$contents[] = $file;
				}
			}
			closedir($folder);
		}
		$clean_dir = (explode("/",$top_dir));
		$clean_dir = $clean_dir[count($clean_dir) - 1];
		$parent_dir = str_replace(array($clean_dir,root_dir),"",$this_dir);
		$parent_dir = substr($parent_dir,0,strlen($parent_dir) - 1);
		$response = "<ul id=\"file_list\">\n";
		if($top_dir != 'drizzo-files') {
			$sub = 'sub';
		}
		if($top_dir != 'drizzo-files') {
			$response .= "\t<li><a href=\"javascript:void(0)\" onclick=\"getFolderContents('" . $parent_dir . "')\" title=\"Go Up a Folder\" class=\"back\">$clean_dir</a></li>\n";
		}
		if(count($contents)>1) {
			foreach($contents as $content) {
				if(is_dir($this_dir ."/" . $content) && $content != 'cache' && $content !='') {
					$response .= "\t<li><a href=\"javascript:void(0)\" onclick=\"getFolderContents('" . str_replace(root_dir,'',$this_dir) . "/" . $content . "')\" class=\"folder $sub\">$content</a></li>\n";
				} elseif(is_file($this_dir . "/" . $content)) {
					$mime = mime_content_type($this_dir . "/" . $content);
					$mime_type = explode("/",$mime);
					if($mime_type[1] == 'application') {
						$mime_type = $mime_type[1];
					} else {
						$mime_type = $mime_type[0];
					}
					if($mime_type == 'image' && strpos($this_dir,'gallery') != false && strpos($this_dir,'thumbs') != true) {
						$response .= "\t<li><a href=\"javascript:void(0)\" onclick=\"mediaLinkConfirm('/" . str_replace(root_dir,'',$this_dir) . "/$content','$content','$mime')\" title=\"Choose insert type for $content\" class=\"$mime_type $sub\">$content</a></li>\n";
					} elseif($mime_type == 'audio') {
						$response .= "\t<li><a href=\"javascript:void(0)\" onclick=\"mediaLinkConfirm('/" . str_replace(root_dir,'',$this_dir) . "/$content','$content','$mime')\" title=\"Choose insert type for $content\" class=\"$mime_type $sub\">$content</a></li>\n";
					} else {
						$response .= "\t<li><a href=\"javascript:void(0)\" onclick=\"insertMediaLink('link','/" . str_replace(root_dir,'',$this_dir) . "/$content','$content')\" title=\"Insert Link for $content\" class=\"$mime_type $sub\">$content</a></li>\n";
					}
				}
			}
		} else {
			$response .= "\t<li>This folder is empty.</li>\n";
		}
		$response .= "</ul>\n";
		echo $response;
	}
?>
